package com.whut.monitor.controller;

import com.whut.monitor.vo.Tree;
import com.whut.monitor.model.CompensatorEquipmentEntity;
import com.whut.monitor.service.ICompensatorEquipmentService;
import com.whut.monitor.vo.ComboVo;
import com.whut.monitor.vo.CompensatorEquipmentVo;
import com.whut.monitor.vo.Grid;
import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;



/**
 * @author CFlower
 * 补偿器、设备关联接口
 */
@Controller
@RequestMapping("/compensatorEquipment")
public class CompensatorEquipmentController {

    @Autowired
    private ICompensatorEquipmentService compensatorEquipmentService;

    @RequestMapping("/manager")
    public String manager() {
        return "/mintor/sensorConfiguration/compensatorEquipment";
    }

    @RequestMapping("/dataGrid")
    @ResponseBody
    public Grid dataGrid(CompensatorEquipmentVo compensatorEquipmentVo, PageFilter pageFilter) {
        Grid grid = new Grid();
        grid.setRows(compensatorEquipmentService.dataGrid(compensatorEquipmentVo, pageFilter));
        grid.setTotal(compensatorEquipmentService.count(compensatorEquipmentVo, pageFilter));
        return grid;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseMsg add(CompensatorEquipmentVo compensatorEquipmentVo) {
        ResponseMsg j = new ResponseMsg();
        try {
            compensatorEquipmentService.add(compensatorEquipmentVo);
            j.setSuccess(true);
            j.setMsg("添加成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ResponseMsg edit(CompensatorEquipmentVo compensatorEquipmentVo) {
        ResponseMsg j = new ResponseMsg();
        try {
            compensatorEquipmentService.edit(compensatorEquipmentVo);
            j.setSuccess(true);
            j.setMsg("编辑成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseMsg delete(Integer id) {
        ResponseMsg j = new ResponseMsg();
        try {
            compensatorEquipmentService.delete(id);
            j.setMsg("删除成功！");
            j.setSuccess(true);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/tree")
    @ResponseBody
    public List<Tree> tree() {
        return compensatorEquipmentService.tree();
    }

    @RequestMapping("/getCompeansatorByEquip")
    @ResponseBody
    public List<ComboVo> getCompeansatorByEquip(Integer equipmentId) {
        List<ComboVo> list=new ArrayList<>();
        List<CompensatorEquipmentEntity> entity=compensatorEquipmentService.getCompeansatorByEquip(equipmentId);
        for (CompensatorEquipmentEntity compensatorEquipmentEntity : entity) {
            ComboVo comboVo=new ComboVo();
            comboVo.setValue(compensatorEquipmentEntity.getId());
            comboVo.setText(compensatorEquipmentEntity.getCompensatorEntity().getCompensatorName());
            comboVo.setNum(compensatorEquipmentEntity.getCompensatorEntity().getCompensatorNum());
            list.add(comboVo);
        }
        return list;
    }

}
